package com.meitianhui.convenitenbuy.module.mine.ui.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.module.mine.adapter.FadeBackPhotoAdapter;
import com.meitianhui.convenitenbuy.module.mine.contract.FadeBackContract;
import com.meitianhui.convenitenbuy.module.mine.di.DaggerMineComponent;
import com.meitianhui.convenitenbuy.module.mine.di.MineModule;
import com.meitianhui.convenitenbuy.module.mine.presenter.FadeBackPresenter;
import com.meitianhui.convenitenbuy.util.RxUtils;
import com.meitianhui.convenitenbuy.util.TipUtils;
import com.meitianhui.convenitenbuy.widget.dialog.BottomMenuDialog;
import com.meitianhui.convenitenbuy.widget.dialog.LoadingDialog;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import static com.meitianhui.convenitenbuy.util.JumpUtil.JUMP_USER_PHONE_NUMBER;

/**
 * Created by amy on 2017/11/16.
 */

public class FadeBackActivity extends BaseToolbarActivity implements FadeBackPhotoAdapter.OnItemClickListener, FadeBackContract.View {
    private EditText mEtFadeBackContent;
    private RecyclerView mRvFadeBack;
    private EditText mEtFadeBackNumber;
    private Button mBtnFadeBackCommit;
    private String mPhoneNumber;
    private List<Uri> photoList = new ArrayList<>();
    private FadeBackPhotoAdapter mFadeBackPhotoAdapter;
    private BottomMenuDialog mBottomMenuDialog;
    @Inject
    FadeBackPresenter mFadeBackPresenter;
    private String mFadeBackContent;
    private String mFadeBackNumber;
    private LoadingDialog imgsUploadingDialog = null;

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        DaggerMineComponent.builder().appComponent(App.getInstance().getAppComponent()).mineModule(new MineModule(this)).build().inject(this);
        setTbTitle("用户反馈");
        initId();
        initData();
        initBottomDialog();
    }

    private void initData() {
        Intent intent = getIntent();
        mPhoneNumber = intent.getStringExtra(JUMP_USER_PHONE_NUMBER);
        mEtFadeBackNumber.setText(mPhoneNumber);

        //初始化recyclerView
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRvFadeBack.setLayoutManager(linearLayoutManager);
        mRvFadeBack.setItemAnimator(new DefaultItemAnimator());
        mRvFadeBack.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.HORIZONTAL));//设置间距

        mFadeBackPhotoAdapter = new FadeBackPhotoAdapter(this, photoList, this);
        mRvFadeBack.setAdapter(mFadeBackPhotoAdapter);
    }

    @Override
    public void call(View v) {
        super.call(v);
        switch (v.getId()) {
            case R.id.btn_fade_back_commit:
                commitFadeBack();
                break;
        }
    }

    private void commitFadeBack() {
        mFadeBackContent = mEtFadeBackContent.getText().toString().trim();
        mFadeBackNumber = mEtFadeBackNumber.getText().toString().trim();
        if (TextUtils.isEmpty(mFadeBackContent)) {
            TipUtils.showToast(App.getInstance(), "请输入用户反馈内容", 3);
            return;
        }
        if (TextUtils.isEmpty(mFadeBackNumber)) {
            TipUtils.showToast(App.getInstance(), "请输入正确的手机号或QQ号", 3);
            return;
        }
        if (photoList.size() != 0) {
            //上传图片
            imgsUploadingDialog = new LoadingDialog(FadeBackActivity.this);
            mFadeBackPresenter.upLoadFadebackPhoto(photoList, mFadeBackNumber, mFadeBackContent);
        } else {
            mFadeBackPresenter.upLoadFadebackContent(mFadeBackContent, mFadeBackNumber);
        }

    }

    private void initId() {
        mEtFadeBackContent = getViewById(R.id.et_fade_back_content);
        mRvFadeBack = getViewById(R.id.rv_fade_back);
        mEtFadeBackNumber = getViewById(R.id.et_fade_back_number);
        mBtnFadeBackCommit = getViewById(R.id.btn_fade_back_commit);

        //initRxJavaClick
        RxUtils.bindEvents(mBtnFadeBackCommit, this);
    }

    @Override
    protected int setView() {
        return R.layout.activity_fade_back;
    }

    @Override
    protected void onRefreshData() {

    }

    /**
     * 点击图片放大
     *
     * @param position
     */
    @Override
    public void onItemClick(int position) {

        if (photoList.size() < 5 && position == mFadeBackPhotoAdapter.getItemCount() - 1) {
            //当点击在添加照片的那张图片上时，弹出对话框
            mBottomMenuDialog.show();
        } else {
            //都是点击到图片上，直接放大图片

        }
    }


    private void initBottomDialog() {
        mBottomMenuDialog = new BottomMenuDialog(this);
        mBottomMenuDialog.addItem(0, getResources().getString(R.string.user_select_photo_resource), null);
        mBottomMenuDialog.addItem(1, getResources().getString(R.string.user_select_camera), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开相机
                mFadeBackPresenter.openCamera(photoList);
                dismissDialog();
            }
        });
        mBottomMenuDialog.addItem(2, getResources().getString(R.string.user_select_photo_album), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开相册
                mFadeBackPresenter.openAlbum(photoList);
                dismissDialog();
            }
        });
        mBottomMenuDialog.addItem(3, getResources().getString(R.string.user_select_cancel), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissDialog();
            }
        });
        mBottomMenuDialog.setTextColor(0, getResources().getColor(R.color.gray));
        mBottomMenuDialog.setTextColor(3, getResources().getColor(R.color.gray));
    }

    /**
     * 图片的回调
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        mFadeBackPresenter.dealWithPicture(requestCode, resultCode, data, photoList);
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void dismissDialog() {
        mBottomMenuDialog.dismiss();
    }

    /**
     * 获取图片成功后，显示图片
     *
     * @param photoList
     */
    @Override
    public void setAdapterAgain(List<Uri> photoList) {
        mFadeBackPhotoAdapter.setData(photoList);
    }

    @Override
    public void upLoadSuccess() {
        finish();
    }

    @Override
    public void upLoadImagSuccess() {
        //上传内容
        if (imgsUploadingDialog != null) {
            imgsUploadingDialog.stopProgressDialog();
        }
        mFadeBackPresenter.upLoadFadebackContent(mFadeBackContent,mFadeBackNumber);
    }

    @Override
    public void uploadImgsFailed() {
        if (imgsUploadingDialog != null) {
            imgsUploadingDialog.stopProgressDialog();
        }
        TipUtils.showToast(FadeBackActivity.this, "图片未能上传，反馈失败。请重试！", 1);
    }

}
